package com.johnson.news.view;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.johnson.news.R;
import com.johnson.news.util.Utils;

import java.util.ArrayList;

public class CommonListPopupWindow extends PopupWindow {
    Context context;
    public CommonListPopupWindow(Context context){
        this.context = context;

//        initView();
    }

    ArrayList<String> dataList = new ArrayList<>();
    public void setData(ArrayList<String> list){
        dataList.clear();
        if(list != null)
            dataList.addAll(list);
    }

    OnFilterClickListener onFilterClickListener;
    public void setOnFilterClickListener(OnFilterClickListener listener){
        onFilterClickListener = listener;
    }

    public void initView() {
        ListView listView = new ListView(context);
        listView.setDivider(null);
        listView.setBackgroundResource(R.mipmap.menu_bg);
        listView.setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, dataList));
        listView.setOnItemClickListener(onItemClickListener);
        setContentView(listView);

        setTouchable(true);
        setOutsideTouchable(true);
        //设置弹框的宽和高
        this.setWidth(Utils.screenWidth * 2 / 5);
        this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable cd = new ColorDrawable(0xb0000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        this.setBackgroundDrawable(new BitmapDrawable());
    }

    AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            dismiss();
            if(onFilterClickListener != null)
                onFilterClickListener.onFilterClick(i);
        }
    };

    public interface OnFilterClickListener{
        public void onFilterClick(int index);
    }
}
